﻿RefreshDropDown = function (event) {
    $('#Issues').append('<div class="loading col-md-offset-2 col-sm-offset-2 saim-sm-2 saim-md-2" style="width:100%;"></div>');
    $('#Issues').load(urlIssues, { Id: $('#Areas-DropDown').val() }, function () {
        $('#Issues').find('div.loading').remove();
        $('#issues-dropdown').addClass('selectpicker');
        $('#issues-dropdown').selectpicker({});
    });
}

$(document).ready(function () {
    $('#Areas-DropDown').selectpicker({});
    $('#Areas-DropDown').bind('change', RefreshDropDown);
    RefreshDropDown(null);

    var users = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: urlUsers,
            wildcard: '%QUERY'
        }
    });

    $('.typeahead').typeahead(null, {
        name: 'users',
        limit: 10,
        display: 'Name',
        source: users,
        templates: {
            empty: [
              '<div class="empty-message">',
                'Ningún usuario encontrado',
              '</div>'
            ].join('\n'),
            suggestion: Handlebars.compile('<div><b>{{UPN}}</b> - {{Name}}</div>')
        }
    });

    jQuery('.typeahead').on('typeahead:selected', function (e, item) {
        var selectedUPN = item.UPN;
        $("#User").val(selectedUPN);
        return item;
    });
});
